home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CTOOLS10.ARJ / HASHTST.C < prev    next >
C/C++ Source or Header  |  1991-12-31  |  2KB  |  88 lines

  1. /****************************************************************************
  2.  
  3.     Filename    :    Hashtest.c
  4.     Version        :    1.0b
  5.     Description    :    Hash table test program
  6.  
  7. ****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <alloc.h>
  13. #include "debug.h"
  14. #include "hash.h"
  15.  
  16. /* An Application that demonstrates how to use the hash functions. Creates
  17.  * a database holding the argv srtings and then prints the database.
  18.  */
  19.  
  20. typedef struct {                    /* A database record */
  21.     char    *key;
  22.     int        other_stuff;
  23.     } ENTRY;
  24.  
  25. /*-------------------------------------------------------------------------*/
  26.  
  27. unsigned hash(ENTRY *sym)            /* Hash function. Convert the key    */
  28. {
  29.     char    *p;
  30.     int        hash_value = 0;
  31.  
  32.     for (p = sym->key; *p; hash_value = (hash_value << 1) ^ (unsigned char)*p++)
  33.         ;
  34.  
  35.     return hash_value;
  36. }
  37.  
  38. /*-------------------------------------------------------------------------*/
  39.  
  40. int cmp(ENTRY *sym1,ENTRY *sym2)    /* Compare two database records.    */
  41. {
  42.     return strcmp(sym1->key,sym2->key);
  43. }
  44.  
  45. /*-------------------------------------------------------------------------*/
  46.  
  47. void print(ENTRY *sym,FILE *stream)    /* Print a database record to the stream    */
  48. {
  49.     fprintf(stream,"%s\n",sym->key);
  50. }
  51.  
  52. /*-------------------------------------------------------------------------*/
  53.  
  54. void main(int argc,char *argv[])
  55. {
  56.     HASH_TAB    *tab;
  57.     ENTRY        *p,*q;
  58.  
  59.     printf("\nMemory at start: %lu\n",(unsigned long)coreleft());
  60.  
  61.     tab = hsh_init(31,hash,cmp);                /* make a hash table    */
  62.     for (++argv, --argc; --argc>=0; argv++)    {     /* For each element of    */
  63.         p = (ENTRY*)hsh_newsym(sizeof(ENTRY));    /* argv, Put it into table    */
  64.         p->key = *argv;
  65.         hsh_addsym(tab,p);
  66.         }
  67.  
  68.     hsh_ptab(tab,print,stdout,0);        /* Print the table. stdout is        */
  69.                                     /* passed through to print().        */
  70.  
  71.     p = q = hsh_newsym(sizeof(ENTRY));
  72.     q->key = "in";
  73.  
  74.     q = hsh_findsym(tab,q);
  75.  
  76.     print(q,stdout);
  77.  
  78.     while ((q = hsh_nextsym(tab,q)) != NULL)    /* Get the next one */
  79.         print(q,stdout);
  80.  
  81.     printf("\nMemory before deleting hash table: %lu\n",(unsigned long)coreleft());
  82.  
  83.     hsh_kill(tab,hsh_freesym);
  84.     hsh_freesym(p);
  85.  
  86.     printf("\nMemory after deleting hash table: %lu\n",(unsigned long)coreleft());
  87. }
  88.